use tauri::Manager;
use crate::core::AppState;

pub mod core;
pub mod db;
pub mod config;
pub mod format;
pub mod fs;
pub mod local_history;
pub mod proc;
pub mod chat;
pub mod ai_timeline;
// 重新启用 watcher 模块
pub mod watcher;
// 新增：search 模块
pub mod search;

pub use core::commands::open_in_new_window;

// -------- Devtools command (available in non-macOS builds when feature enabled)
#[tauri::command]
fn open_devtools(window: tauri::WebviewWindow) {
    #[cfg(not(target_os = "macos"))]
    window.open_devtools();
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        // 注册官方插件：对话框与系统打开器
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_opener::init())
        .setup(|app| {
            // 初始化应用状态和数据库路径
            let app_data_dir = app.path().app_data_dir().expect("Failed to get app data directory");
            let db_path = app_data_dir.join("app.db");
            let app_state = AppState::new_with_path(db_path);
            app.manage(app_state);
            
            // 初始化文件系统监听相关
            let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<watcher::WatcherCommand>();
            let manager = watcher::FileWatcherManager::new(app.handle().clone());
            tauri::async_runtime::spawn(manager.run(rx));
            app.manage(tx);

            // 启动时自动打开 DevTools（仅非 macOS）
            #[cfg(not(target_os = "macos"))]
            if let Some(w) = app.get_webview_window("main") {
                w.open_devtools();
            }
            Ok(())
        })
        .invoke_handler(tauri::generate_handler![
            config::set_model_config,
            config::get_model_config,
            config::list_model_keys,
            config::delete_model_config,
            config::set_app_setting,
            config::get_app_setting,
            chat::chat,
            fs::fs_read_text,
            fs::fs_write_text,
            fs::fs_read_dir,
            fs::fs_mkdir,
            fs::fs_delete,
            fs::fs_rename,
            fs::fs_stat,
            format::format_code,
            proc::run_command,
            proc::pty_spawn,
            proc::pty_resize,
            proc::pty_write,
            proc::pty_read,
            proc::pty_kill,
            proc::list_shells,
            local_history::timeline::get_timeline,
            local_history::local_provider::save_local_history,
            local_history::local_provider::get_local_history_content,
            local_history::local_provider::set_history_config,
            local_history::local_provider::get_history_config_value,
            local_history::local_provider::cleanup_all_history,
            local_history::git_provider::get_git_history_content,
            local_history::local_history_restore,
            open_devtools,
            core::commands::open_in_new_window,
            // 重新启用 watcher 命令
            watcher::start_watching_directory,
            watcher::stop_watching_directory,
            watcher::stop_all_watching,
            // 新增：文件快速搜索命令
            search::search_files,
            // 新增：内容全文搜索流式命令
            search::start_content_search,
            search::cancel_content_search,
            // AI timeline database-backed commands
            ai_timeline::ai_timeline_add,
            ai_timeline::ai_timeline_list,
            ai_timeline::ai_timeline_clear,
            ai_timeline::ai_timeline_clear_from,
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
